package com.ikangtai.shecare.curve;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.baseView.TopBar;
import com.ikangtai.shecare.common.eventBusMsg.CurveMsg;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.ButtonUtil;
import com.ikangtai.shecare.main.MainActivity;
import com.jaeger.library.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subcriber;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class BBTChartActivity extends SwipeBackActivity implements View.OnClickListener {


    private WebView webview;
    private TopBar topBar;
    private ImageView topBar_back, share;
    private RelativeLayout Horizontal_topbar;
    private SaveChartImg2Local saveChartImg2Local;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // appKilledFlag 为 -1 说明程序被杀掉
        if (App.appKilledFlag == -1) {
            AppLog.i("shecare is killed by Android OS! now begin reStart!");
            protectApp();
        } else
            AppLog.i("shecare normally start!");

        setContentView(R.layout.bbtchart_layout);

        SwipeBackLayout mSwipeBackLayout = getSwipeBackLayout();
        //设置滑动方向，可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

        EventBus.getDefault().register(this);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.lpq_color), 0);
        initView();
        App.activityList.add(this);
    }

    protected void protectApp() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//清空栈里MainActivity之上的所有activty
        startActivity(intent);
        finish();
    }

    private final void initView() {

        webview = (WebView) findViewById(R.id.webview);
        loadWebview();
        topBar_back = (ImageView) findViewById(R.id.topBar_back);
        share = (ImageView) findViewById(R.id.share);
        share.setOnClickListener(this);
        Horizontal_topbar = (RelativeLayout) findViewById(R.id.Horizontal_topbar);
        topBar = (TopBar) findViewById(R.id.topBar);

        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

            topBar.setVisibility(View.GONE);
            Horizontal_topbar.setVisibility(View.VISIBLE);
            // 横屏

        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

            topBar.setVisibility(View.VISIBLE);
            Horizontal_topbar.setVisibility(View.GONE);

            // 竖屏

        }

        topBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void midLeftClick() {
            }

            @Override
            public void midRightClick() {
            }

            @Override
            public void rightClick() {
                if (ButtonUtil.isFastDoubleClick()) { // 按钮点击次数控制
                    return;
                } else {
                    AppLog.i("get User Msg And Draw Table");
                    Toast.makeText(BBTChartActivity.this, R.string.BBTToast, Toast.LENGTH_SHORT).show();
                    saveChartImg2Local = new SaveChartImg2Local();
                    saveChartImg2Local.getUserMsgAndDrawTable(BBTChartActivity.this);
                }
            }
        });
        topBar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Subcriber
    public void onCurveMsg(CurveMsg curveMsg) {
        webview.loadUrl("file:///android_asset/health/bbtChart.html");
    }

    private void loadWebview() {
        WebViewCfg webViewCfg = new WebViewCfg(webview);
        webview = webViewCfg.getCfgedWebView(this);
        webview.loadUrl("file:///android_asset/health/bbtChart.html");

//        webview.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                int w = View.MeasureSpec.makeMeasureSpec(0,
//                        View.MeasureSpec.UNSPECIFIED);
//                int h = View.MeasureSpec.makeMeasureSpec(0,
//                        View.MeasureSpec.UNSPECIFIED);
//                //重新测量
//                webview.measure(w, h);
//            }
//        });

//        wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        wv.setVerticalScrollBarEnabled(false);
//        wv.setVerticalScrollbarOverlay(false);
//        wv.setHorizontalScrollBarEnabled(false);
//        wv.setHorizontalScrollbarOverlay(false);

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
            BBTChartActivity.this.finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //必须捕捉，否则Activity onDestroy然后onCreate，js不能有效的监听到横竖屏的变化;
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 当新设置中，屏幕布局模式为横排时
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //TODO 横屏，调整webview为全屏
            AppLog.i("BBTChartActivity is now ORIENTATION_LANDSCAPE");
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            //TODO 竖屏，调整webview和下半部分的比例
            AppLog.i("BBTChartActivity is now ORIENTATION_PORTRAIT");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    //防止内存泄露
    @Override
    protected void onDestroy() {
        webview.destroy();
        webview.removeAllViews();
        EventBus.getDefault().unregister(this);
        super.onDestroy();

        App.activityList.remove(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share:
                if (ButtonUtil.isFastDoubleClick(R.id.share)) { // 按钮点击次数控制
                    return;
                } else {
                    AppLog.i("get User Msg And Draw Table");
                    Toast.makeText(BBTChartActivity.this, R.string.BBTToast, Toast.LENGTH_SHORT).show();
                    saveChartImg2Local = new SaveChartImg2Local();
                    saveChartImg2Local.getUserMsgAndDrawTable(BBTChartActivity.this);
                }
                break;
        }
    }
}
